package com.wheat.vipcard.bean;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.wheat.vipcard.utils.PageResultSet;
import com.wheat.vipcard.utils.PageUtil;

/**
 * A data access object (DAO) providing persistence and search support for
 * Cardtype entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.wheat.vipcard.bean.Cardtype
 * @author MyEclipse Persistence Tools
 */

public class CardtypeDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(CardtypeDAO.class);
	// property constants
	public static final String TYPE_CODE = "typeCode";
	public static final String TYPE_NAME = "typeName";
	public static final String TYPE_OF_CODE = "typeOfCode";

	public void save(Cardtype transientInstance) {
		log.debug("saving Cardtype instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Cardtype persistentInstance) {
		log.debug("deleting Cardtype instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Cardtype findById(java.lang.Integer id) {
		log.debug("getting Cardtype instance with id: " + id);
		try {
			Cardtype instance = (Cardtype) getSession().get("com.wheat.vipcard.bean.Cardtype", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<?> findByExample(Cardtype instance) {
		log.debug("finding Cardtype instance by example");
		try {
			List<?> results = getSession().createCriteria("com.wheat.vipcard.bean.Cardtype")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<?> findByProperty(String propertyName, Object value) {
		log.debug("finding Cardtype instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Cardtype as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<?> findByTypeCode(Object typeCode) {
		return findByProperty(TYPE_CODE, typeCode);
	}

	public List<?> findByTypeName(Object typeName) {
		return findByProperty(TYPE_NAME, typeName);
	}

	public List<?> findByTypeOfCode(Object typeOfCode) {
		return findByProperty(TYPE_OF_CODE, typeOfCode);
	}

	public List<?> findAll() {
		log.debug("finding all Cardtype instances");
		try {
			String queryString = "from Cardtype";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Cardtype merge(Cardtype detachedInstance) {
		log.debug("merging Cardtype instance");
		try {
			Cardtype result = (Cardtype) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Cardtype instance) {
		log.debug("attaching dirty Cardtype instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Cardtype instance) {
		log.debug("attaching clean Cardtype instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/**
	 * 分页查询
	 * 
	 * @return
	 */
	public PageResultSet<Cardtype> findCardTypeByPage(Cardtype type, int pageNo, int pageSize) {
		PageResultSet<Cardtype> page = new PageResultSet<Cardtype>();
		try {
			String sqlString = bulidQuerySQL(type);
			int totalRow = queryCount(sqlString);
			PageUtil pages = new PageUtil(totalRow, pageSize, pageNo);
			List<Cardtype> list = new ArrayList<Cardtype>();
			List<Object[]> cList = queryList(sqlString, pageNo, pageSize);
			for (Object[] b : cList) {
				Cardtype ct = new Cardtype();
				ct.setId(b[0] == null ? 0 : Integer.parseInt(b[0].toString()));
				ct.setTypeName(b[2] == null ? "" : b[2].toString());
				ct.setTypeCode(b[1] == null ? 0 : Integer.parseInt(b[1].toString()));
				ct.setTypeOfCode(b[3] == null ? 0 : Integer.parseInt(b[3].toString()));
				list.add(ct);
			}
			page.setList(list);
			page.setPage(pages);
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error("System error!!!", e);
			}
			e.printStackTrace();
		} finally {

		}
		return page;

	}

	/**
	 * 构建查询SQL
	 * 
	 * @param type
	 * @return
	 */
	public String bulidQuerySQL(Cardtype type) {
		String sqlStr = "select * from cardtype ct where 1=1 ";
		if (type != null) {
			if (StringUtils.isNotBlank(type.getTypeName())) {
				sqlStr += " and ct.typeName like'" + type.getTypeName() + "%'";
			}
			if (type.getTypeCode() != 0) {
				sqlStr += " and ct.typeCode ='" + type.getTypeCode() + "%'";
			}
		}
		return sqlStr;
	}
}